package com.qiaofang.common.utils;

import com.qiaofang.common.model.PageDTO;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;


public class PageUtil {

    public static Pageable getPageByDTO(PageDTO dto) {
        if (dto == null) {
            dto = new PageDTO();
        }
        int size = dto.getPageSize();
        if (size == 0) {
            size = 1;
        }
        int num = dto.getPageNum();
        if (num == 0) {
            num = 1;
        }
        Pageable pageable = PageRequest.of(num - 1, size);
        return pageable;
    }

    public static Pageable getPageByDTO(PageDTO dto, Sort sort) {
        if (dto == null) {
            dto = new PageDTO();
        }
        int size = dto.getPageSize();
        if (size == 0) {
            size = 1;
        }
        int num = dto.getPageNum();
        if (num == 0) {
            num = 1;
        }
        Pageable pageable = PageRequest.of(num - 1, size, sort);
        return pageable;
    }

    public static Sort getCreateTimeDesc() {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        return sort;
    }

    public static Sort getCreateTimeAsc() {
        Sort sort = new Sort(Sort.Direction.ASC, "createTime");
        return sort;
    }
}
